var totalElements;
$(function () {

    var fatherId=0;
    var pageNo=1;
    var pageSize=10;
    var number=$("#number").val();
    page( $("#totalElements").val(),number,10,0);

    //点击分类展示所有子类
    $(".layui-nav").on("click", "li", function () {
        fatherId = $(this).children("span").text();
        location.href="/showtype/listType?pageNo="+pageNo+"&pageSize="+pageSize+"&fatherId="+fatherId;
    });
    $('#city_add').on('click', function(){
        layer.open({
            type: 1,
            title:'添加类别',
            area: ['600px',''],
            shadeClose: false,
            content: $('#add')
        });
        selectBigType();

    });



});
//把所有类别绑定
function selectBigType(){
    $.getJSON("/showtype/father","fatherId=0",function(data){
        $("#fatherType").append("<option value='0'>请选择类型名称</option>");
        $.each(data,function(i,e){
            $("#fatherType").append("<option value='"+e.showTypeId+"'>"+e.typeName+"</option>");
        });
    });
}
//删除子id
function del(showTypeId,fatherId){
  /* layer.msg(''+fatherId, {
        time: 2000, //2s后自动关闭
    });*/
    if(fatherId==0){
        layer.msg('父类别不可删除！~', {
            time: 2000, //2s后自动关闭
        });
    }else{
        layer.confirm("确认要删除吗？",{btn:['确定','取消']},function () {
           /* layer.msg('执行删除！~', {
                time: 2000, //2s后自动关闭
            });*/
          $.ajax({
                type:"POST",
                url:"/showtype/delete" ,
                data:{showTypeId:showTypeId},
                dataType:"text",
                success:function(data){

                    if(data==1){
                        layer.msg('删除成功！', {
                            time: 2000, //2s后自动关闭
                        });
                        setTimeout(function () {
                            location.reload();
                        },1000);
                    }
                },
                error:function () {
                    //配置一个透明的询问框
                    layer.msg('删除失败！尝试刷新后重试。', {
                        time: 2000, //2s后自动关闭
                    });
                }
            });
        },function () {
            layer.msg('用户取消操作', {
                time: 1500, //1.5s后自动关闭
            });
        });
    }
}

function  page(totalElements,pageNo,pageSize,fatherId){
    layui.use('laypage',function () {
        var curr= 0;
        curr= parseInt($("#number").text())+1;
        var laypage=layui.laypage
        laypage.render({
            elem:'demo1',
            curr:curr,
            count:totalElements,
            layout:['count','prev','page','next','refresh','skip'],
            limit:pageSize,//每一页显示的条数
            jump:function (obj,first) {
                //ojb包含了当前分页的所有参数
               /* console.log(obj.curr);
                console.log(obj.limit);//得到当前页，一边向服务端请求对应页的数据*/
                if(!first){
                    location.href="/showtype/listType?pageNo="+obj.curr+"&pageSize="+obj.limit+"&fatherId="+fatherId;
                }
            }
        })
    })
}